/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wasolicitudesfiec.xsl.pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.fop.svg.PDFTranscoder;
import wasolicitudesfiec.logicacontrol.Logica;
import web.util.HtmlEntities;

/**
 *
 * @author kabad
 */
public class Transformador {

    private String xml;
    private String svg;

    public Transformador() {
    }
    //este metodo toma el xsl plantilla y crea un objeto transformer
    //con el objeto transformer creo el svg, con el archivo xml de datos

    public void doIt(String idModelo, String xmlDatos, String path) throws TransformerConfigurationException, IOException, TransformerException, TranscoderException, Exception {
        String plantillaXsl = "";
        TransformerFactory tFactory = TransformerFactory.newInstance();
        javax.xml.transform.Transformer transformer;
        File f = new File(path + ".pdf");
        HtmlEntities htmlEntities = new HtmlEntities();
        if (idModelo.equals(Logica.modelo_SolicitarCambioCarrera)) {
            plantillaXsl = "c:\\Informe_Cambio_Carrera.xsl";
        }
        try {
            //no generar el archivo si ya existe
            if (!f.exists()) {
                //aplicamos los htmlentities, para poder trabajar con tildes, y caracteres especiales
                xml = htmlEntities.convertirISOSymbolEntities(xmlDatos);
                xml = htmlEntities.convertirISOCharacterEntities(xml);
                //            xml = htmlEntities.convertirOtherEntities(xmlDatos);
                transformer = tFactory.newTransformer(new StreamSource(plantillaXsl));
                File file = new File("informe.xml");
                FileWriter fw = new FileWriter(file);
                fw.write(xml);
                fw.flush();
                fw.close();

                // 3.Usar el transformador para procesar el origen de datos XML
                // y obtener el resultado de la transformaciï¿½n
                transformer.transform(new StreamSource("informe.xml"), new StreamResult(new FileOutputStream(path + ".svg")));
//	            transformer.transform(new StreamSource("C:\\Informe_Cambio_Carrera.xml"), new StreamResult(new FileOutputStream("C:\\Informe_Cambio_Carrera.svg")));

                //de svg a pdf
                File svgfile = new File(path + ".svg");
                File pdffile = new File(path + ".pdf");
                convertSVG2PDF(svgfile, pdffile);
                svgfile.delete();
            }
        } catch (Exception ex) {
            throw ex;
        }

    }

    public void convertSVG2PDF(File svg, File pdf) throws IOException, TranscoderException {

        //Create transcoder
        Transcoder transcoder = new PDFTranscoder();
        //Transcoder transcoder = new org.apache.fop.render.ps.PSTranscoder();

        //Setup input
        InputStream in = new java.io.FileInputStream(svg);
        try {
            TranscoderInput input = new TranscoderInput(in);
            //Setup output
            OutputStream out = new java.io.FileOutputStream(pdf);
            out = new java.io.BufferedOutputStream(out);
            try {
                TranscoderOutput output = new TranscoderOutput(out);
                org.apache.batik.util.XMLResourceDescriptor.setXMLParserClassName("org.apache.xerces.parsers.SAXParser");

                //Do the transformation
                transcoder.transcode(input, output);
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }

    public String getXml() {
        return xml;
    }

    public void setXml(String xml) {
        this.xml = xml;
    }

    public String getSvg() {
        return svg;
    }

    public void setSvg(String svg) {
        this.svg = svg;
    }
}
